import json
from django.utils.deprecation import MiddlewareMixin
from rest_framework_simplejwt.authentication import JWTAuthentication
from labor_price.models import *
from django.http import JsonResponse

class MD1(MiddlewareMixin):
    def process_request(self, request):
        # print(request.path, "路径")
        # print(request.method, "请求方式")
        # print(request.COOKIES, "COOKIES")
        # print(request.META.get("HTTP_AUTHORIZATION"), "请求头")
        # 解码token, 获取用户名


        if request.path == "/pc/login":
            print(request.body)
            uses = json.loads(request.body)
            print(uses['username'])
            # 获取用户名和部门
            try:
                users = list(People.objects.filter(username=uses["username"]).values("name", "Role__department__name"))[0]
                # 获取当前所操作的模块
                urls = list(Menus.objects.filter(urls=request.path).values("title"))[0]
                Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                   operateObj=users["Role__department__name"],
                                   operateType="登录")
            except IndexError:
                return JsonResponse({"msg": "用户异常", "code": '401'})
            # return JsonResponse({"ip": request.META['REMOTE_ADDR']})
        else:

            if request.method == 'GET':
                return
            else:
                jwt_obj = JWTAuthentication()
                tokens = request.META.get("HTTP_AUTHORIZATION").split(" ")[1]
                validated_token = jwt_obj.get_validated_token(tokens)
                user = jwt_obj.get_user(validated_token)
                # 获取用户名和部门
                users = list(People.objects.filter(username=user).values("name", "Role__department__name"))[0]
                # 获取当前所操作的模块

                print(request.path)
                try:
                    urls = list(Menus.objects.filter(urls=request.path).values("title"))[0]
                    if request.method == 'POST':
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="添加")
                    elif request.method == "PUT":
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="编辑")
                    elif request.method == "DELETE":
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="删除")
                    elif request.method == "PATCH":
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="编辑")
                    elif request.method == "OPTIONS":
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="编辑")
                except IndexError:
                    li = request.path.split("/")
                    menus = '/'+li[1] + '/' + li[2]
                    urls = list(Menus.objects.filter(urls=menus).values("title"))[0]
                    if request.method == 'POST':
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="添加")
                    elif request.method == "PUT":
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="编辑")
                    elif request.method == "DELETE":
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="删除")
                    elif request.method == "PATCH":
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="编辑")
                    elif request.method == "OPTIONS":
                        Log.objects.create(name=users["name"], belongToModel=urls["title"],
                                           operateObj=users["Role__department__name"],
                                           operateType="编辑")
        # print("md1  process_request 方法。", id(request))  # 在视图之前执行
